package com.sgb.kjwl.view.widget.pick;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;

/**
 * 地图app选择
 */
public class PickMapAppWindow extends BasePopWindow {

    private Double latD = 0.0;
    private Double logD = 0.0;
    private String addressMap;
    private TextView baidu;
    private TextView gaode;
    private TextView tenxun;
    private TextView cancel;
    private boolean isShowBaidu;
    private boolean isShowGaode;
    private boolean isShowTenxun;

    public PickMapAppWindow(Context context,int dir) {
        super(context,dir);
    }

    @Override
    protected int popLayout() {
        return R.layout.pick_map_app;
    }

    @Override
    protected void initView(PopViewHolder holder) {

        baidu = holder.getPop().findViewById(R.id.map_baidu);
        gaode = holder.getPop().findViewById(R.id.map_gaode);
        cancel = holder.getPop().findViewById(R.id.cancel);
        tenxun = holder.getPop().findViewById(R.id.map_tenxun);

        baidu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpBaiDuMap(latD,logD,addressMap);
            }
        });
        gaode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpGaoDeMap(latD,logD,addressMap);
            }
        });
        tenxun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpTenXunMap(latD,logD,addressMap);
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    public void setData(Double lat,Double log,String address){
        latD = lat;
        logD = log;
        addressMap = address;
    }

    public void setShowData(boolean showBaidu,boolean showGaode,boolean showTenxun){
        isShowBaidu = showBaidu;
        isShowGaode = showGaode;
        isShowTenxun = showTenxun;
        if(isShowBaidu){
            baidu.setVisibility(View.VISIBLE);
        }
        if(isShowGaode){
            gaode.setVisibility(View.VISIBLE);
        }
        if(isShowTenxun){
            tenxun.setVisibility(View.VISIBLE);
        }
        
    }


    /**
     * 调用高德地图
     *
     *   Uri.parse("androidamap://route?sourceApplication=" + R.string.app_name
     *                 + "&dlat=" + dlat
     *                 + "&dlon=" + dlon
     *                 + "&dname=" + dname
     *                 + "&dev=0&0t=0")
     * @param
     * @return
     */
    protected void jumpGaoDeMap(double latitude, double longitude, String address) {
        String url = "androidamap://route?sourceApplication=" + R.string.app_name + "&sname=我的位置&dlat=" + latitude + "&dlon=" + longitude + "&dname=" + address + "&dev=0&m=0&t=0";
        Intent intent = new Intent("android.intent.action.VIEW",
                android.net.Uri.parse(url));
        mContext.startActivity(intent);
    }

    /**
     * 调用百度地图
     * Uri.parse("baidumap://map/direction?origin=我的位置&destination=name:"
     *                 + dname
     *                 + "|latlng:" + dlat + "," + dlon
     *                 + "&index=0&target=1")
     *
     *   mode = transit（公交）、driving（驾车）、walking（步行）和riding（骑行）. 默认:driving
     *  * 当 mode=transit 时 ： sy = 0：推荐路线 、 2：少换乘 、 3：少步行 、 4：不坐地铁 、 5：时间短 、 6：地铁优先
     *
     * @param
     * @return
     */
    protected void jumpBaiDuMap(double latitude, double longitude, String address) {
        String url = "baidumap://map/direction?origin=我的位置&destination=name:" + address+"|latlng:"+latitude + "," + longitude + "&index=0&target=1&src=andr.baidu.openAPIdemo";
        Intent intent = new Intent("android.intent.action.VIEW",
                android.net.Uri.parse(url));
        mContext.startActivity(intent);
    }

    /**
     * 调用腾讯地图
     *  qqmap://map/routeplan?type=drive&to=中关村&tocoord=39.9836,116.3164&policy=1&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77
     * @param
     * @return
     */
    protected void jumpTenXunMap(double endLatitude, double endLongitude, String endAddress) {
        String url = "qqmap://map/routeplan?type=drive&to=" + endAddress + "&tocoord=" + endLatitude + "," + endLongitude + "&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77";
        Intent intent = new Intent("android.intent.action.VIEW",
                android.net.Uri.parse(url));
        mContext.startActivity(intent);
    }
}
